package com.zhang.controller.goods;

import com.zhang.goods.entity.Attrgroup;
import com.zhang.common.util.result.R;
import com.zhang.common.util.result.RUtils;
import com.zhang.goods.entity.AttrAttrgroup;
import com.zhang.goods.service.IAttrAttrgroupService;
import com.zhang.goods.service.IAttrgroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 属性分组信息表 前端控制器
 * </p>
 *
 * @author 创世神
 * @since 2023-09-09
 */
@RestController
@RequestMapping("/attrgroup/")
@Slf4j
public class AttrgroupController {
    @Autowired
    private IAttrgroupService attrgroupService;//属性分组表

    @Autowired
    private IAttrAttrgroupService assoService;//属性分组与属性关联表

    @PostMapping("add")
    R addAttrGroup(@Valid @RequestBody Attrgroup attrgroup) {
        //TODO 此处需要检查是不是三级分类，如果不是就不能添加
        boolean save = attrgroupService.save(attrgroup);
        return save ? RUtils.successMsg("新增成功") : RUtils.err("新增失败");
    }

    @PostMapping("add/asso")
    R addAssoAttrGroup(@Valid @RequestBody List<AttrAttrgroup> asso) {
        boolean save = assoService.saveBatch(asso);
        return save ? RUtils.successMsg("关联成功") : RUtils.err("关联失败");
    }

    @GetMapping("get/byCategoryId/{categoryId}")
    R getAttrGroupByCategoryId(@PathVariable Long categoryId) {
        List<Attrgroup> list = attrgroupService.listByCategoryId(categoryId);
        return RUtils.successData(list);
    }
    @GetMapping("get/group/{id}")
    public R getGroupInfo(@PathVariable Long id){
        Attrgroup byId = attrgroupService.getById(id);
        return RUtils.successData(byId);
    }

    @PutMapping("modify/group")
    R modifyGroup(@RequestBody Attrgroup attrgroup) {
        attrgroupService.modifyGroup(attrgroup);
        return RUtils.successMsg("修改成功");
    }
    @DeleteMapping("del/group/list")
    R delGroup(@RequestBody List<Long>  attrgroupIds) {
        attrgroupService.removeBatchByIds(attrgroupIds);
        return RUtils.successMsg("删除成功");
    }
}
